#include "envelope.h"

#include <algorithm>

Envelope::Envelope()
    :xmin_(0.0),xmax_(0.0),ymin_(0.0),ymax_(0.0)
{
}

Envelope::Envelope(double xmin, double xmax, double ymin, double ymax)
    :xmin_(std::min(xmin,xmax)),xmax_(std::max(xmin,xmax))
    ,ymin_(std::min(ymin,ymax)),ymax_(std::max(ymin,ymax))
{

}

bool Envelope::IsIntersect(const Envelope &e1, const Envelope &e2)
{
    double xmin_in_max = std::min(e1.xmax(),e2.xmax());
    double xmax_in_min = std::max(e1.xmin(),e2.xmin());
    if(xmin_in_max > xmax_in_min)
        return false;

    double ymin_in_max = std::min(e1.ymax(),e2.ymax());
    double ymax_in_min = std::min(e1.ymin(),e2.ymax());
    if(ymin_in_max > ymax_in_min)
        return false;

    return true;
}

bool Envelope::IsIntersect(const Envelope &other) const
{
    return Envelope::IsIntersect(*this,other);
}
